﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using sortof;

namespace SkipList
{
    class Class1
    {
        [STAThread] // single thread execution
        static void Main(string[] args)
        {
            /* We will test the skip list with many keys. */
            int n = 50000;

            /* Prepare the keys and the values. Make sure the keys are unique. */
            //Console.WriteLine("Preparing {0} random unique numbers...", n);
            int[] uniqueNumbers = new int[n];
            Int32[] keys = new Int32[n];
            object[] values = new object[n];
            Random r = new Random();
            bool unique = true;
            int x = 0;
            for (int i = 0; i < n; i++)
            {
                /* Each number we generate has to be unique. So after we generate a new
                 * number, we compare it with all previously generated numbers and if it is 
                 * not unique then we regenerate it. */
                do
                {
                    x = r.Next();
                    unique = true;
                    for (int j = 0; j < i; j++)
                    {
                        if (uniqueNumbers[j] == x)
                        {
                            unique = false;
                            break;
                        }
                    }
                } while (!unique);
                uniqueNumbers[i] = x;

                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("{0}", x);

                keys[i] = Int32.Parse(sb.ToString());
                values[i] = sb.ToString();

                Console.Write("\r{0}", keys[i]);
            }
            Console.WriteLine("\n{0} random numbers generated. Now running inserts selects and deletes:", n);


            /* ------------------------------------------------
             * Test and measure execution time for skip list. 
             * ------------------------------------------------
             */
            SkipList slist = new SkipList();


            CodeTijdMeting.setup();
            CodeTijdMeting.begin();

            //DateTime start = DateTime.Now;

            /* Make insertions. */
            for (int i = 0; i < n; i++)
                slist.Insert(keys[i], values[i]);

            /* Make searches. */
            for (int i = 0; i < n; i++)
            {
                object obj = slist.Find(keys[i]);
                if (obj == null)
                    throw new Exception("Null value for key ->" + keys[i] + "<-");

                string stringValue = (string)obj;
                if (!stringValue.Equals(values[i]))
                    throw new Exception("Wrong value for key ->" + keys[i] + "<-");
            }

            /* Make removals. */
            for (int i = 0; i < n; i++)
                slist.Remove(keys[i]);

            //DateTime stop = DateTime.Now;

            //Console.WriteLine("SkipList execution time: " + (stop - start));
            
            CodeTijdMeting.stop();
            Console.WriteLine("Time: " + CodeTijdMeting.getResultNanoSecond() / 1000000000);
            Console.WriteLine("Begin: " + CodeTijdMeting.getStart());
            Console.WriteLine("End: " + CodeTijdMeting.getEnd());
            Console.WriteLine("Freq: " + CodeTijdMeting.getFrequency());
            CodeTijdMeting.reset();

            Console.ReadKey();
        }
    }
}
